<template>
  <div>
    <button @click="handleOpenBaiDuMap">打开百度导航</button>
    <button @click="handleOpenGaoDeiMap">打开高德导航</button>
  </div>
</template>

<script>
import { isAndroid, isIOS } from "@/utils";
export default {
  methods: {
    handleOpenBaiDuMap() {
      if (isAndroid) {
        //这个是安卓操作系统
        window.location.href =
          "androidamap://viewMap?sourceApplication=appname&poiname=abc&lat=36.2&lon=116.1&dev=0";
        //判断是否切出浏览器
        setTimeout(function () {
          let hidden =
            window.document.hidden ||
            window.document.mozHidden ||
            window.document.msHidden ||
            window.document.webkitHidden;
          if (typeof hidden == "undefined" || hidden == false) {
            //应用宝下载地址
            window.location.href =
              "https://uri.amap.com/marker?position=116.473195,39.993253";
          }
        }, 2500);
      }
      if (isIOS) {
        //这个是ios操作系统
        window.location.href =
          "iosamap://viewMap?sourceApplication=appname&poiname=abc&lat=36.2&lon=116.1&dev=0";
        //判断是否切出浏览器
        setTimeout(function () {
          let hidden =
            window.document.hidden ||
            window.document.mozHidden ||
            window.document.msHidden ||
            window.document.webkitHidden;
          if (typeof hidden == "undefined" || hidden == false) {
            //应用宝下载地址
            window.location.href =
              "https://uri.amap.com/marker?position=116.473195,39.993253";
          }
        }, 2500);
      }

      // let params = {
      //   cityName: "苏州市",
      //   curLat: "31.303216",
      //   curLng: "120.592942",
      //   currAddr: "姑苏区三香路975号",
      //   elat: "31.30532",
      //   elng: "120.627521",
      //   eaddr: "南门",
      // };
      // openBaiDuMap(params);
    },
    handleOpenGaoDeiMap() {
      if (isAndroid) {
        //这个是安卓操作系统
        let d = new Date();
        let t0 = d.getTime();
        window.location.href =
          "androidamap://viewMap?sourceApplication=appname&poiname=abc&lat=36.2&lon=116.1&dev=0";
        //由于打开需要1～2秒，利用这个时间差来处理－－打开app后，返回h5页面会出现页面变成app下载页面，影响用户体验
        var delay = setInterval(function () {
          let d = new Date();
          let t1 = d.getTime();
          if (t1 - t0 < 3000 && t1 - t0 > 2000) {
            window.location.href =
              "https://uri.amap.com/marker?position=116.473195,39.993253";
          }
          if (t1 - t0 >= 3000) {
            clearInterval(delay);
          }
        }, 1000);
      }
      if (isIOS) {
        //这个是ios操作系统
        let d = new Date();
        let t0 = d.getTime();
        window.location.href =
          "iosamap://viewMap?sourceApplication=appname&poiname=abc&lat=36.2&lon=116.1&dev=0";
        //由于打开需要1～2秒，利用这个时间差来处理－－打开app后，返回h5页面会出现页面变成app下载页面，影响用户体验
        let delay = setInterval(function () {
          let d = new Date();
          let t1 = d.getTime();
          if (t1 - t0 < 3000 && t1 - t0 > 2000) {
            window.location.href =
              "https://uri.amap.com/marker?position=116.473195,39.993253";
          }
          if (t1 - t0 >= 3000) {
            clearInterval(delay);
          }
        }, 1000);
      }

      // openGaoDeiMap();
    },
  },
};
</script>

<style lang="scss" scoped></style>
